package com.hhkj.api.train.service.extend.impl;

import com.google.common.collect.Lists;
import com.hhkj.admin.train.domain.Train;
import com.hhkj.admin.train.model.dopo.TrainDo;
import com.hhkj.admin.train.service.base.TrainService;
import com.hhkj.api.common.session.holder.ApiStudentSessionHolder;
import com.hhkj.api.common.session.holder.model.LoginStudent;
import com.hhkj.api.train.common.cont.ApiTrainConst;
import com.hhkj.api.train.model.info.res.HistoryTrainRes;
import com.hhkj.api.train.service.extend.ApiStudentTrainHistoryExtendService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MathUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 历史培训 Service
 *
 * @author RuoYi
 * @since 2023/6/8
 */
@Service
public class ApiStudentTrainHistoryExtendServiceImpl implements ApiStudentTrainHistoryExtendService {
    @Autowired
    private TrainService trainService;

    /**
     * 分页查询历史测试记录
     *
     * @return 历史测试记录
     */
    @Override
    public List<HistoryTrainRes> pageHistoryTest() {
        List<Train> currentStudentTrainList = getCurrentStudentTrainList(ApiTrainConst.TypeCode.TEST.getCode());
        HistoryTrainRes historyTestRes;
        List<HistoryTrainRes> historyTestResList = Lists.newArrayList();
        for (Train historyTest : currentStudentTrainList) {
            historyTestRes = new HistoryTrainRes();
            historyTestRes.setId(historyTest.getId());
            historyTestRes.setMark(historyTest.getMark());
            historyTestRes.setSubmitDate(historyTest.getSubmitTime());
            historyTestRes.setDurationSecond(historyTest.getDurationSecond());
            historyTestRes.setCostTime(DateUtils.getFormatDate(historyTest.getDurationSecond()));
            historyTestRes.setAnswerAmount(historyTest.getAnswerAmount());
            historyTestRes.setRightAmount(historyTest.getRightAmount());
            historyTestResList.add(historyTestRes);
        }
        return historyTestResList;
    }

    /**
     * 分页查询历史练习记录
     *
     * @return 历史练习记录
     */
    @Override
    public List<HistoryTrainRes> pageHistoryPractice() {
        List<Train> currentStudentTrainList = getCurrentStudentTrainList(ApiTrainConst.TypeCode.PRACTISE.getCode());
        HistoryTrainRes historyPracticeRes;
        List<HistoryTrainRes> historyPracticeResList = Lists.newArrayList();
        for (Train historyPractice : currentStudentTrainList) {
            historyPracticeRes = new HistoryTrainRes();
            historyPracticeRes.setId(historyPractice.getId());
            historyPracticeRes.setPracticeInfo(StringUtils.format("共{}道题 做对{}道", historyPractice.getAnswerAmount(), historyPractice.getRightAmount()));
            historyPracticeRes.setSubmitDate(historyPractice.getSubmitTime());
            historyPracticeRes.setCostTime(DateUtils.getFormatDate(historyPractice.getDurationSecond()));
            historyPracticeRes.setDurationSecond(historyPractice.getDurationSecond());
            historyPracticeRes.setAnswerAmount(historyPractice.getAnswerAmount());
            historyPracticeRes.setRightAmount(historyPractice.getRightAmount());
            String rightRate = MathUtils.calculateThePercentage(historyPractice.getRightAmount(), historyPractice.getAnswerAmount());
            historyPracticeRes.setRightRate(rightRate);
            historyPracticeResList.add(historyPracticeRes);
        }
        return historyPracticeResList;
    }

    /**
     * 获取当前登录学员的培训情况列表
     *
     * @param typeCode 做题类型 practise练习 test考试
     * @return
     */
    private List<Train> getCurrentStudentTrainList(String typeCode) {
        LoginStudent loginStudent = ApiStudentSessionHolder.getLoginStudent();
        TrainDo trainDo = new TrainDo();
        trainDo.setStudentId(loginStudent.getId());
        trainDo.setCourseId(loginStudent.getCourseId());
        trainDo.setTypeCode(typeCode);
        return trainService.listStudentTrainListPoByDo(trainDo);
    }
}
